package member.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

public class LoginCheckInterceptor implements HandlerInterceptor{

	@Override
	//view처리 종료 후에 호출
	//3 : Object - Handler 메소드 (HandlerMethod)
	//4 : Exception - 발생한 오류
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
					throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	//Handler(controller 메소드) 처리 종료
	//4. ModelAndView - handler메소드가 reutrn 한 ModelAndView
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	//Handler 처리 전에 호출
	//return boolean : false 리턴 -> 다음으로 이동하지 않는다.
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		System.out.println("preHandle() 실행");
		//로그인 여부를 사전 체크
		HttpSession session = request.getSession();
		Object loginInfo = session.getAttribute("login_info");
		if(loginInfo == null){//로그인 X
			ModelAndView mv = new ModelAndView("member/login_form.jsp", "error_message", "로그인을 먼저 하세요.");
			throw new ModelAndViewDefiningException(mv);// return ModelAndView 를 한것과 동일.(error.jsp로 이동)
		}
		return true;
	}

}
